/*******************************************************************************
*   			Impacts of a Large-Scale Parenting Program: 				   *
*					Experimental Evidence from Chile						   *
********************************************************************************


	REQUIRES:	"${data}Panel_RR_complete.dta"
	CREATES:	${data}/Panel_RR_indexes.tex
				
	WRITEN BY:  Italo Lopez  [italolop@usc.edu]

	
********************************************************************************
*	Load data on Health Centers
*******************************************************************************/

	
	import excel "${dt_raw}/center_types.xlsx", sheet("Sheet1") firstrow clear
	label def centrotype 1 "Family Health Center" 2 "General Health Center" 3 "Small Hospital"
	label values CENTRO_TYPE centrotype 
	tempfile center_type
	save `center_type', replace

********************************************************************************
*	Prepare data and merge health centers
*******************************************************************************/

	
	* Load data
	use "${dt_final}/Panel_RR_complete.dta", clear
		
	/* This data set includes baseline and follow up surveys. It also includes 
	   information on attrition, takeup, facilicitators, and center types. */
	   
	   
	merge m:1 CENTRO_SALUD_old using `center_type'
	drop if _m==2
	drop _m

	tabulate CENTRO_TYPE, gen (centype)
	rename centype1 familyhc
	rename centype2 generalhc
	rename centype3 hospital
	label def familyhc 0 "Family HC" 1 "General HC or Hospital" 
	label values familyhc familyhc
	label def generalhc 0 "General HC" 1 "Family HC or Hospital" 
	label values generalhc generalhc
	label def hospital 0 "Hospital" 1 "General/Family HC" 
	label values hospital hospital
	   
/*******************************************************************************
*   Set macros for regressions
*******************************************************************************/
	
	* Outcomes
	*-----------------------------------------------------------
	local childoutcomes  "zDCCSPunt zptevir_irtscore2 zt_score zcbcl_t_e zcbcl_t_i zibatt_sspi zibatt_ssai zibatt_sssr zas_raw_score" 
	local behaviors "home_score se_score disc_score" 
	local beliefs "zpstyle1 zpstyle2 zpstyle3 zPSCS zPSSS_family zPSSS_friends zPSSS_others zPSI_Distress zCESD zpacotis"	
	
	* Controls
	*-----------------------------------------------------------

	macro  def Xvar1 "i.age_year i.gender"  	
	macro  def Xvar2 "i.age_year i.gender inc_qtaut_old i.pc_edu_mdsfin hh_mem hh_tipo"  	
	macro  def Xvar2old "i.gender inc_qtaut_old i.pc_edu_mdsfin_old hh_mem hh_tipo"  

	
	* Parental characteristics: WAIS and BFI and imputed wais 
	*-----------------------------------------------------------

	macro def parent_cha4 "BFI_ext BFI_ope BFI_con BFI_neu BFI_agr wais2 flag_wais"
	

/*******************************************************************************
	Create relevant variables
*******************************************************************************/

	xtset CENTRO_SALUD // Establish panel data 

	* Create 3 age groups
	g age_yeargsem=. 
	replace age_yeargsem=1 if inrange(age_year,3,5)
	replace age_yeargsem=2 if inrange(age_year,6,7)
	replace age_yeargsem=3 if age_year == 8

	rename DCCSPunt dccspunt
	rename zDCCSPunt zdccspunt


	local list "ibatt_rpi ibatt_rai raw_total_aud raw_total_exp"
	foreach var of local list {
		replace `var'=. if age_year>5 
	}
	replace ibatt_rsr=. if age_year == 8
	
	
	
/*******************************************************************************
	Index creation 
*******************************************************************************/
	
	* Single child index
	*-----------------------------------------------------------

	gsem (dccspunt ptevir_irtscore2 as_raw_score ibatt_rpi ibatt_rsr 		  /*
	*/ ibatt_rai cbcl_t_i cbcl_t_e <- X)  if age_yeargsem  ==  1, var(X@1) 
	predict child_index_1 if e(sample), latent 

	gsem (dccspunt <- X@2.123105 _cons@14.97192) 							  /*
	*/ (ptevir_irtscore2 <- X@0.4557614 _cons@-0.3747977) 					  /*
	*/ (as_raw_score <- X@5.072063 _cons@38.89723)  						  /*
	*/ (cbcl_t_i <- X) (cbcl_t_e <- X)  									  /*
	*/ (ibatt_rsr <- X) 													  /*
	*/ if age_yeargsem == 2, mean(X) 
	predict child_index_2 if e(sample), latent  
	

	gsem (dccspunt <- X@2.123105 _cons@14.97192)  							  /*
	*/ (ptevir_irtscore2 <- X@0.4557614 _cons@-0.3747977)  					  /*
	*/ (as_raw_score <- X@5.072063 _cons@38.89723)  						  /*
	*/ (cbcl_t_i <- X) (cbcl_t_e <- X) if age_yeargsem == 3, mean(X) 
	predict child_index_3 if e(sample), latent  

	
	gen child_index=.
	replace child_index=child_index_1 if age_yeargsem == 1
	replace child_index=child_index_2 if age_yeargsem == 2
	replace child_index=child_index_3 if age_yeargsem == 3
	drop child_index_1 child_index_2 child_index_3
	

	* Executive Function index
	*---------------------------------------------

	gsem (dccspunt ptevir_irtscore2 as_raw_score <- X), var(X@1) 
	matrix b = e(b)
	gsem (dccspunt as_raw_score <- X), var(X@1) from(b,skip)
	predict cog_index if e(sample), latent 

	* Vocabulary Index 
	*---------------------------------------------

	gsem (ss_audi_PLSIV ss_expre_PLSIV ptevir_irtscore2 <- X), var(X@1) 
	matrix b = e(b)
	predict voc_index if e(sample), latent 


	* Socio-emotional index
	*---------------------------------------------

	gsem (ibatt_rpi ibatt_rsr ibatt_rai cbcl_t_i cbcl_t_e <- X) 			  /*
	*/ if age_yeargsem == 1, var(X@1) 
	matrix b = e(b)
	predict child_index_1 if e(sample), latent 

	gsem (cbcl_t_i <- X) 													  /*
	*/ (cbcl_t_e <- X) 														  /*
	*/ (ibatt_rsr <- X@11.20055 _cons@66.4282)								  /*
	*/ if age_yeargsem>=2, mean(X) from(b, skip)   
	matrix b = e(b)
	predict child_index_2 if e(sample), latent  

	gen soc_index=.
	replace soc_index=child_index_1 if age_yeargsem == 1
	replace soc_index=child_index_2 if age_yeargsem>=2
	drop child_index_1 child_index_2 
	

	*  Standarization
	*---------------------------------------------

	local array "child_index cog_index soc_index voc_index"

	local i = 1
	local n: word count `array'
	while `i' <= `n' {
		local var : word `i' of `array'
		bys agecat2m: egen mn`var' = mean(`var') 
		bys agecat2m: egen sd`var' = sd(`var') 
		gen z`var' = (`var'- mn`var')/sd`var' 
		drop mn`var' 
		drop sd`var' 
		label var z`var'  "z score `var', 2 mo inc"
		local i = `i' + 1
	}


/*******************************************************************************
	Create child indexes at baseline  
*******************************************************************************/


	rename DCCSPunt_old dCCSPunt_old

	gsem (dCCSPunt_old pepebdisoc_old raw_total_aud_old raw_total_exp_old     /*
	*/ cbcl_t_i_old cbcl_t_e_old <- X), var(X@1)
	matrix b = e(b)
	predict child_index_old

	gsem (pepebdisoc_old <- X) if age_year_old == 0, var(X@1)
	predict child_index_1
	
	
	gsem (pepebdisoc_old <- X@7.219528 _cons@20.73036) 						  /* 
	*/ (cbcl_t_i_old <- X) if age_year_old >= 1, from(b, skip)
	predict child_index_2
	
	gen soc_index_old = .
	replace soc_index_old = child_index_1 if age_year_old == 0
	replace soc_index_old = child_index_2 if age_year_old >= 1


	gen agemo_old = EDAD_MESES_x_old
	gen agemo2_old = EDAD_MESES_x_old * EDAD_MESES_x_old
	gen agemo3_old = agemo2_old * EDAD_MESES_x_old

	local child "child_index_old soc_index_old"
	foreach var of local child {

		reg `var' agemo_old agemo2_old agemo3_old
		predict `var'_xb if e(sample), xb 
		predict `var'_res if e(sample), res
		gen `var'_ressq = `var'_res^2
		
		reg `var'_ressq agemo_old agemo2_old agemo3_old
		predict `var'_ressq_xb if e(sample), xb 
		replace `var'_ressq_xb = 0 if `var'_ressq_xb<0 
		
		gen `var'_std = sqrt(`var'_ressq_xb)
		gen z`var' = (`var'-`var'_xb)/`var'_std
	}

	egen mindex = median(zchild_index_old)
	gen lindex = (zchild_index_old < mindex) if zchild_index_old!=.
	gen hindex= 1 - lindex
	gen intlindex2 = lindex * (TIPO_old == 2)
	gen intlindex3 = lindex * (TIPO_old == 3)
	gen inthindex2 = hindex * (TIPO_old == 2)
	gen inthindex3 = hindex * (TIPO_old == 3)

	egen msocindex = median(zsoc_index_old)
	gen lsocindex = (zsoc_index_old < msocindex) if zsoc_index_old != .
	gen hsocindex= 1 - lsocindex
	gen intlsocindex2 = lsocindex * (TIPO_old == 2)
	gen intlsocindex3 = lsocindex * (TIPO_old == 3)
	gen inthsocindex2 = hsocindex * (TIPO_old == 2)
	gen inthsocindex3 = hsocindex * (TIPO_old == 3)

	egen mefindex = median(zDCCSPunt_old)
	gen lefindex = (zDCCSPunt_old < mefindex) if zDCCSPunt_old!=.
	gen hefindex= 1 - lefindex
	gen intlefindex2 = lefindex * (TIPO_old == 2)
	gen intlefindex3 = lefindex * (TIPO_old == 3)
	gen inthefindex2 = hefindex * (TIPO_old == 2)
	gen inthefindex3 = hefindex * (TIPO_old == 3)


	save "${dt_final}/panel_RR_indexes.dta", replace


******************************** The end ***************************************
